<template lang="en">
    <div class="demo">
        <h2>学校名称：{{name}}</h2>
        <h2>学校地址：{{address}}</h2>
        <button @click="showName">点我提示学校名</button>
        <button @click='sendSchoolName'>点我把学校名给App</button>
    </div>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
    name: 'School',
    data() {
        return {
            name: '尚硅谷',
            address: '北京昌平'
        }
    },
    methods: {
        showName() {
            alert(this.name)
            // 解绑School组件自定义事件
             this.$off('atguigu');
        },
        sendSchoolName() {
            // 触发School组件实例身上的atguigu事件
            this.$emit('atguigu',this.name,this.address);
        },
        unbind() {
            this.$off('atguigu')
        }
    },
    mounted() {
        // 事件总线
        // this.$bus.$on('hello',(data)=>{
        //     console.log("我是School组件，收到了数据",data);
        // })

        // 消息订阅
        this.pubId = pubsub.subscribe('hello',(msgName,data) => {
            console.log('有人发布消息了',msgName,data);
        })
    },
     beforeDestroy() {
            // this.$bus.$off('hello')
            pubsub.unsubscribe(this.pubId)
    }
}
</script>
<style>
    .demo {
        background-color: orangered;
    }
</style>